package cn.antho.shop.web.controller;

import cn.antho.shop.domain.OutputWarnningLog;
import cn.antho.shop.domain.Product;
import cn.antho.shop.domain.ShopBuy;
import cn.antho.shop.dto.InputDto;
import cn.antho.shop.feign.BuyService;
import cn.antho.shop.feign.ProductService;
import cn.antho.shop.mapper.OutputWarnningLogMapper;
import cn.antho.shop.service.IShopOutputOrInputLogService;
import cn.antho.shop.domain.ShopOutputOrInputLog;
import cn.antho.shop.query.ShopOutputOrInputLogQuery;
import cn.antho.shop.result.JsonResult;
import cn.antho.shop.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/shopOutputOrInputLog")
public class ShopOutputOrInputLogController {

    @Autowired
    public IShopOutputOrInputLogService shopOutputOrInputLogService;

    @Autowired
    private ProductService productService;

    @Autowired
    private BuyService buyService;

    @Autowired
    private OutputWarnningLogMapper outputWarnningLogMapper;


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody ShopOutputOrInputLog shopOutputOrInputLog){
        if(shopOutputOrInputLog.getId()!=null){
            shopOutputOrInputLogService.updateById(shopOutputOrInputLog);
        }else{
            shopOutputOrInputLogService.insert(shopOutputOrInputLog);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        shopOutputOrInputLogService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(shopOutputOrInputLogService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(shopOutputOrInputLogService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody ShopOutputOrInputLogQuery query){
        Page<ShopOutputOrInputLog> page = new Page<ShopOutputOrInputLog>(query.getPage(),query.getRows());
        page = shopOutputOrInputLogService.selectPage(page);
        return JsonResult.success(new PageList<ShopOutputOrInputLog>(page.getTotal(),page.getRecords()));
    }

    @RequestMapping(value = "/input",method = RequestMethod.POST)
    public JsonResult input(@RequestBody InputDto dto) {
        ShopOutputOrInputLog shopOutputOrInputLog = new ShopOutputOrInputLog();
        shopOutputOrInputLog.setType(dto.getPid());
        shopOutputOrInputLog.setState(dto.isBoo());
        shopOutputOrInputLog.setCount(dto.getNum());
        shopOutputOrInputLogService.insert(shopOutputOrInputLog);
        System.out.println(shopOutputOrInputLog);
        if (true == shopOutputOrInputLog.getState()) {
            Product product = productService.getByProductId(shopOutputOrInputLog.getType());
            System.out.println(product);
            product.setStock(product.getStock() + shopOutputOrInputLog.getCount());
            productService.update(product);
        } else if (false == shopOutputOrInputLog.getState()) {
            Product product = productService.getByProductId(shopOutputOrInputLog.getType());
            if (product.getStock() < shopOutputOrInputLog.getCount()) {
                return JsonResult.error("库存不足！");
            }
            product.setStock(product.getStock() - shopOutputOrInputLog.getCount());
            productService.update(product);
            if ((product.getStock() - shopOutputOrInputLog.getCount()) <= 10) {
                OutputWarnningLog outputWarnningLog = new OutputWarnningLog();
                outputWarnningLog.setType(dto.getPid());
                outputWarnningLog.setCount(dto.getNum());
                outputWarnningLog.setCreatetime(new Date());
                outputWarnningLog.setLog("库存量小于10个！请及时补货！");
                outputWarnningLogMapper.insert(outputWarnningLog);
                ShopBuy shopBuy = new ShopBuy();
                shopBuy.setType(shopOutputOrInputLog.getType());
                shopBuy.setCount(50);
                shopBuy.setState(0);
                buyService.add(shopBuy);
            }
        }
        return JsonResult.success();
    }

}
